package com.hxd.mall.utils.open

import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.util.Base64
import com.hxd.mall.constant.HxdConstants.Sd_Card_Pic_Folder
import com.hxd.mall.constant.HxdGlobalInfo
import java.io.*

object FileUtil {

    /**
     * base64编码字符串转换为图片
     *
     * @param imgStr base64编码字符串
     * @return
     */
    fun base64StrToImage(base64Code: String) {
        if (TextUtils.isEmpty(base64Code) || base64Code.length < 5) {
            return
        }

        var out: FileOutputStream? = null
        try {
            val path = Sd_Card_Pic_Folder + System.currentTimeMillis() + ".png"
            val imgStrTemp = base64Code.substring(base64Code.indexOf(",") + 1)
            val bytes = Base64.decode(imgStrTemp, Base64.DEFAULT)
            //文件夹不存在则自动创建
            val tempFile = File(path)
            if (!tempFile.parentFile.exists()) {
                tempFile.parentFile.mkdirs()
            }
            out = FileOutputStream(tempFile)
            out.write(bytes)
            HxdGlobalInfo.instance.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://$path")))
        } catch (e: Exception) {
        } finally {
            if (out != null) {
                out.flush()
                out.close()
            }
        }
    }

    /**
     * 以追加方式写文件(上层确保路径和文件存在)
     *
     * @param path
     * @param str
     */
    fun writeAppendToFile(path: String, str: String) {
        var writer: FileWriter? = null
        if (path == null || str == null) {
            return
        }

        val file = File(path)
        if (!file.exists()) {
            var isCreateFileSuccess = false
            try {
                isCreateFileSuccess = file.createNewFile()
            } catch (e: IOException) {
                e.printStackTrace()
            } finally {
                if (!isCreateFileSuccess) {
                    return
                }
            }
        }

        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = FileWriter(path, true)
            writer.write(str)
            writer.write("\r\n")
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            if (writer != null) {
                try {
                    writer.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }

            }
        }
    }

    /**
     * 获取assert文件
     * assert file
     */
    fun getAssertFileStr(filePath: String): String {
        var result = ""
        var inputStream: InputStream? = null
        try {
            inputStream = HxdGlobalInfo.instance.resources.assets.open(filePath)
            val buffer = ByteArray(inputStream!!.available())
            inputStream.read(buffer)
            result = String(buffer, charset("utf-8"))

        } catch (e: Exception) {

        } finally {
            try {
                inputStream?.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        return result
    }
}

